﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightCountDownTimer.ViewModel;
using System.ComponentModel;
using SilverlightCountDownTimer.Utils;
using System.Windows.Controls;

/// <summary>
/// Created by Jacek Ciereszko
/// http://jacekciereszko.pl/
/// </summary>
/// 
namespace SilverlightTimer
{    
    public partial class SilverlightTimerControl : UserControl
    {
        #region Fields
        /// <summary>
        /// This event will be invoked when timer finish counting.
        /// </summary>
        public event EventHandler CounterFinish;
        #endregion

        #region constructor
        public SilverlightTimerControl()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(SilverlightTimerControl_Loaded);
        }
        #endregion

        #region Control loaded
        void SilverlightTimerControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (EventTimeCache)
            {
                ((SilverlightTimerControlViewModel)this.DataContext).EventTimeCacheDuration = EventTimeCacheDuration;
            }
            ((SilverlightTimerControlViewModel)this.DataContext).EventTimeCache = EventTimeCache;
            if (EventTimeUrl != String.Empty && EventTime.Equals(DateTime.MinValue))
            {
                ((SilverlightTimerControlViewModel)this.DataContext).EventTimeUrl = EventTimeUrl;
            }
            if (!EventTime.Equals(DateTime.MinValue))
            {
                ((SilverlightTimerControlViewModel)this.DataContext).EventTime = EventTime;
            }
            ((SilverlightTimerControlViewModel)this.DataContext).CounterFinish += CounterFinish;
            ((SilverlightTimerControlViewModel)this.DataContext).Description = EventDescription; 
        }
        #endregion

        #region DependencyProperties

        #region EventTime
        /// <summary>
        /// Event time. When it's set, counter count time to this date.
        /// </summary>
        [TypeConverter(typeof(SilverlightCountDownTimer.Utils.DateTimeTypeConverter))]
        public DateTime EventTime
        {
            get { return (DateTime)GetValue(EventTimeProperty); }
            set { SetValue(EventTimeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EventTime.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EventTimeProperty =
            DependencyProperty.Register("EventTime", typeof(DateTime), typeof(SilverlightTimerControl), new PropertyMetadata(DateTime.MinValue, OnEventDateChanged));

        static void OnEventDateChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            ((SilverlightTimerControl)source).UpdateEventTime((DateTime)args.NewValue);
        }

        private void UpdateEventTime(DateTime eventTime)
        {
            ((SilverlightTimerControlViewModel)this.DataContext).EventTime = eventTime;
        }
        #endregion

        #region EventTimeUrl
        /// <summary>
        /// Url address to txt file where event's time is saved. Thia parametere is optional.
        /// </summary>
        public String EventTimeUrl
        {
            get { return (String)GetValue(EventTimeUrlProperty); }
            set { SetValue(EventTimeUrlProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EventTimeUrl.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EventTimeUrlProperty =
            DependencyProperty.Register("EventTimeUrl", typeof(String), typeof(SilverlightTimerControl), new PropertyMetadata(String.Empty, OnEventDateUrlChanged));

        static void OnEventDateUrlChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            ((SilverlightTimerControl)source).UpdateEventTimeUrl((String)args.NewValue);
        }

        private void UpdateEventTimeUrl(String eventTimeUrl)
        {
            if (EventTime.Equals(DateTime.MinValue))
            {
                ((SilverlightTimerControlViewModel)this.DataContext).EventTimeUrl = eventTimeUrl;
            }
        }
        #endregion

        #region Cache mode
        /// <summary>
        /// Turon on/off caching for event's date.
        /// </summary>
        public Boolean EventTimeCache
        {
            get { return (Boolean)GetValue(EventTimeCacheProperty); }
            set { SetValue(EventTimeCacheProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EventTimeCache.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EventTimeCacheProperty =
            DependencyProperty.Register("EventTimeCache", typeof(Boolean), typeof(SilverlightTimerControl), new PropertyMetadata(false, OnEventTimeCacheChanged));

        static void OnEventTimeCacheChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            ((SilverlightTimerControl)source).UpdateEventTimeCache((Boolean)args.NewValue);
        }

        private void UpdateEventTimeCache(Boolean eventTimeCache)
        {
            ((SilverlightTimerControlViewModel)this.DataContext).EventTimeCache = eventTimeCache;
        }

        /// <summary>
        /// Number of days when event's date will be cached
        /// </summary>
        public int EventTimeCacheDuration
        {
            get { return (int)GetValue(EventTimeCacheDurationProperty); }
            set { SetValue(EventTimeCacheDurationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EventTimeCacheDuration.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EventTimeCacheDurationProperty =
            DependencyProperty.Register("EventTimeCacheDuration", typeof(int), typeof(SilverlightTimerControl), new PropertyMetadata(1, OnEventTimeCacheDurationChanged));

        static void OnEventTimeCacheDurationChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            ((SilverlightTimerControl)source).UpdateEventTimeCacheDuration((int)args.NewValue);
        }

        private void UpdateEventTimeCacheDuration(int eventTimeCacheDuration)
        {
            ((SilverlightTimerControlViewModel)this.DataContext).EventTimeCacheDuration = eventTimeCacheDuration;
        }

        #endregion

        #region Event Description
        public String EventDescription
        {
            get { return (String)GetValue(EventDescriptionProperty); }
            set { SetValue(EventDescriptionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for EventDescription.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty EventDescriptionProperty =
            DependencyProperty.Register("EventDescription", typeof(String), typeof(SilverlightTimerControl), new PropertyMetadata(String.Empty, OnEventDescriptionChanged));

        static void OnEventDescriptionChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            ((SilverlightTimerControl)source).UpdateEventDescription((String)args.NewValue);
        }

        private void UpdateEventDescription(String eventDescription)
        {
            ((SilverlightTimerControlViewModel)this.DataContext).Description = eventDescription;            
        }
        #endregion

        #region Tooltip
        public String Tooltip
        {
            get { return (String)GetValue(TooltipProperty); }
            set { SetValue(TooltipProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Tooltip.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TooltipProperty =
            DependencyProperty.Register("Tooltip", typeof(String), typeof(SilverlightTimerControl), new PropertyMetadata(String.Empty, OnTooltipChanged));

        static void OnTooltipChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            ((SilverlightTimerControl)source).SetValue(ToolTipService.ToolTipProperty, (String)args.NewValue);
        }
        #endregion

        #endregion
    }
}
